package com.hqu.environment.utils;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @Description: 短信发送工具类
 * @Date: 2019/05/15 12:14
 * @Author: 邓勇嘉
 **/
public class SmsUtil {
    private static String operation = "/industrySMS/sendSMS";
    private static String BASE_URL="https://api.miaodiyun.com/20150822";
    private static String ACCOUNT_SID = "254d7c0b552c451fa8773e72b02d2dea";
    private static String AUTH_TOKEN ="173c385dbc7040958f63e46439032864";
    private static final String RESP_DATA_TYPE = "json";
    private static String smsContent1 = "【环境监控科技】登录验证码：";
    private static String smsContent2 ="，如非本人操作，请忽略此短信。";
    /**
     *
     * @param phone 发送手机号码
     * @param code  验证码
     */
    public static void execute(String phone, String code)
    {
        String tmpSmsContent = null;
        try{
            tmpSmsContent = URLEncoder.encode("尊敬的用户，您的验证码为"+code, "UTF-8");
        }catch(Exception e){

        }
        String url = BASE_URL + operation;
        String body = "accountSid=" + ACCOUNT_SID + "&to=" + phone + "&smsContent=" + tmpSmsContent
                + createCommonParam();

        // 提交请求
        String result = post(url, body);
        System.out.println("result:" + System.lineSeparator()+ result);
    }

    /**
     * 加密发送的签名信息
     * @return
     */
    public static String createCommonParam()
    {
        // 时间戳
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timestamp = sdf.format(new Date());

        // 签名
        String sig = DigestUtils.md5Hex(ACCOUNT_SID + AUTH_TOKEN + timestamp);

        return "&timestamp=" + timestamp + "&sig=" + sig + "&respDataType=" + RESP_DATA_TYPE;
    }

    public static String post(String url, String body)
    {
        System.out.println("url:" + System.lineSeparator() + url);
        System.out.println("body:" + System.lineSeparator() + body);

        String result = "";
        try
        {
            OutputStreamWriter out = null;
            BufferedReader in = null;
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();

            // 设置连接参数
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(20000);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 提交数据
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(body);
            out.flush();

            // 读取返回数据
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line = "";
            boolean firstLine = true; // 读第一行不加换行符
            while ((line = in.readLine()) != null)
            {
                if (firstLine)
                {
                    firstLine = false;
                } else
                {
                    result += System.lineSeparator();
                }
                result += line;
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }
}
